<?php

namespace app\adminapi\controller\v1\system;

use app\adminapi\controller\AuthController;
use app\dao\system\attachment\SystemAttachmentDao;
use app\generator\CuteNicknameGenerator;
use app\Request;
use app\services\posts\PostCommentsServices;
use app\services\posts\PostsServices;
use app\services\system\AppRoutingServices;
use app\services\user\UserServices;
use think\facade\App;

/**
 *
 */
class AppRoutingController extends AuthController
{
    /**
     * PostsServices constructor.
     * @param App $app
     * @param PostsServices $service
     * @method temp
     */
    public function __construct(App $app, AppRoutingServices $service)
    {
        parent::__construct($app);
        $this->services = $service;
    }

    public function list(Request $request){
        $dataList = $this->services->selectDataList(['is_del' => 0]);
        foreach ($dataList as &$item) {
            $item['slider_image'] = json_decode($item['slider_image'], true);
            $item['module'] = json_decode($item['module'], true);
        }
        return app("json")->success("查询成功", $dataList);
    }

    public function add(Request $request){
        $where = $request->postMore([
            ['name', ''],
            ['pid', 0],
            ['url', ''],
            ['slider_image', []],
            ['module', []]
        ]);
        $where['slider_image'] = json_encode($where['slider_image']);
        $where['module'] = json_encode($where['module']);
        $this->services->save($where);
        return app("json")->success("添加成功");
    }

    public function edit(Request $request){
        $where = $request->postMore([
            ['id', 0],
            ['name', ''],
            ['pid', 0],
            ['url', ''],
            ['module', []],
            ['slider_image', []]
        ]);
        $where['slider_image'] = json_encode($where['slider_image']);
        $where['module'] = json_encode($where['module']);
        $this->services->update($where['id'], $where);
        return app("json")->success("修改成功");
    }

    public function del(Request $request, $id){
        $this->services->update($id, ['is_del' => 1]);
        return app("json")->success("删除成功");
    }
}